﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using OfficeOpenXml;

namespace Topro.Admin.Controllers
{
    using Microsoft.Extensions.Configuration;
    using System.Linq;
    using Topro.Application.Interface;
    using Topro.Entity.Query;
    using Topro.Util;

    /// <summary>
    /// 设备数据控制器
    /// </summary>
    public class DeviceDataController : Controller
    {
        #region 注入
        /// <summary>
        /// 
        /// </summary>
        private readonly IWebHostEnvironment _hostEnvironment;

        /// <summary>
        /// 设备数据接口
        /// </summary>
        private readonly DeviceDataService _deviceDataService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hostEnvironment">IQS接口</param>
        /// <param name="deviceDataService">设备数据接口</param>
        public DeviceDataController(IWebHostEnvironment hostEnvironment, DeviceDataService deviceDataService)
        {
            _hostEnvironment = hostEnvironment;
            _deviceDataService = deviceDataService;
        }
        #endregion

        #region 首页
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index()
        {
            ViewBag.Password = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build()["data:password"];

            ViewBag.StartTime = DateTime.Now.ToString("yyyy-MM-dd");
            ViewBag.EndTime = DateTime.Now.ToString("yyyy-MM-dd");

            return View();
        }
        #endregion

        #region 分页查询
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>视图</returns>
        [HttpGet]
        public IActionResult DeviceDataQuery(DeviceDataQuery query)
        {
            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                if (DateUtil.IsDateTime(query.StartTime) && DateUtil.IsDateTime(query.EndTime))
                {
                    query.StartTime = Convert.ToDateTime(query.StartTime).ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = Convert.ToDateTime(query.EndTime).ToString("yyyy-MM-dd 23:59:59");
                }
                else
                {
                    query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                }
            }
            else
            {
                query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            }


            var list = _deviceDataService.GetDeviceDataPager(query);

            return PartialView("_Query", list);
        }
        #endregion

        #region 导出Excel
        /// <summary>
        /// 设备数据导出
        /// </summary>
        public IActionResult ExportExcel(DeviceDataQuery query)
        {
            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                if (DateUtil.IsDateTime(query.StartTime) && DateUtil.IsDateTime(query.EndTime))
                {
                    query.StartTime = Convert.ToDateTime(query.StartTime).ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = Convert.ToDateTime(query.EndTime).ToString("yyyy-MM-dd 23:59:59");
                }
                else
                {
                    query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                }
            }
            else
            {
                query.StartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            }

            //获取数据
            var list = _deviceDataService.GetDeviceDataExport(query);
            if (!list.Any())
            {
                return View();
            }
            //文件名称
            var fileName = @$"{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
            //路径
            var filePath = Path.Combine(@$"{_hostEnvironment.WebRootPath}", fileName);
            //文件对象
            FileInfo file = new FileInfo(filePath);
            //是否存在
            if (file.Exists)
            {
                file.Delete();
            }
            //指定EPPlus使用非商业证书
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            using (ExcelPackage package = new ExcelPackage(file))
            {
                //创建sheet
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("OEE");
                //头部
                worksheet.Cells[1, 1].Value = "设备区域";
                worksheet.Cells[1, 2].Value = "异常内容";
                worksheet.Cells[1, 3].Value = "设备状态";
                worksheet.Cells[1, 4].Value = "开始时间";
                worksheet.Cells[1, 5].Value = "结束时间";
                //行数
                int row = 2;

                foreach (var item in list)
                {
                    #region 内容
                    var stringMessage = item.Message;
                    if (item.Index == -1)
                    {
                        stringMessage = "";
                    }
                    #endregion

                    #region 状态
                    var statusString = "";

                    if (item.Status == 1)
                    {
                        statusString = "工作";
                    }
                    else if (item.Status == 2)
                    {
                        statusString = "空闲";
                    }
                    else if (item.Status == 3)
                    {
                        statusString = "异常";

                        if (item.Index == -1)
                        {
                            statusString = "";
                        }
                    }
                    else if (item.Status == 4)
                    {
                        statusString = "换料";
                    }
                    else if (item.Status == 5)
                    {
                        statusString = "班保养";
                    }
                    else if (item.Status == 6)
                    {
                        statusString = "周保养";
                    }
                    else if (item.Status == 7)
                    {
                        statusString = "堵满本";
                    }
                    else if (item.Status == 8)
                    {
                        statusString = "待空底板";
                    }
                    else if (item.Status == 9)
                    {
                        statusString = "换钢板";
                    }
                    else if (item.Status == 10)
                    {
                        statusString = "换铜箔";
                    }
                    else if (item.Status == 11)
                    {
                        statusString = "换PP";
                    }
                    else if (item.Status == 12)
                    {
                        statusString = "无板生产";
                    }
                    else if (item.Status == 13)
                    {
                        statusString = "禁止生产";
                    }
                    else if (item.Status == 14)
                    {
                        statusString = "计划停机";
                    }
                    #endregion

                    worksheet.Cells[row, 1].Value = item.DeviceLine == 20 ? "组合区" : "PP区";
                    worksheet.Cells[row, 2].Value = stringMessage;
                    worksheet.Cells[row, 3].Value = statusString;
                    worksheet.Cells[row, 4].Value = item.StartTime.ToString();
                    worksheet.Cells[row, 5].Value = item.EndTime.ToString();
                    row++;
                }
                package.Save();
            }

            return File(new FileStream(filePath, FileMode.Open), "application/octet-stream", fileName);
        }
        #endregion
    }
}
